package com.oocl.Homework1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CountTextImpl implements CountText {
	
	private static final CountTextImpl cl = new CountTextImpl();
	private CountTextImpl() {

	}

	public static CountTextImpl getInstance() {
		return cl;
	}
	/*
	 * 统计数字
	 */
	public int countNumber(String str) {
		int count = 0;
		Pattern p = Pattern.compile("[0-9]");
		Matcher m = p.matcher(str);
		while (m.find()) {
			count++;
		}
		return count;
	}

	/*
	 * 统计字母
	 */
	public int countLetter(String str) {
		int count = 0;
		Pattern p = Pattern.compile("[a-zA-Z]");
		Matcher m = p.matcher(str);
		while (m.find()) {
			count++;
		}
		return count;
	}

	/*
	 * 统计汉字
	 */
	public int countWord(String str) {
		int count = 0;
		Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]");
		Matcher m = p.matcher(str);
		while (m.find()) {
			count++;
		}
		return count;
	}

	/*
	 * 统计标点符号
	 */
	public int countSymbol(String str) {
		int count = 0;
		Pattern p = Pattern.compile("[,.。?：)]");
		Matcher m = p.matcher(str);
		while (m.find()) {
			count++;
		}
		return count;
	}

	public String getFromText() throws FileNotFoundException {
		Scanner scanner = new Scanner(new FileInputStream("text"));
		String string = null;
		while (scanner.hasNext()) {
			string+=scanner.nextLine();
		}
		return string;
	} 
}


